package com.zh.note.demo.javaSE;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.zh.note.annotation.CustomTest;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class AutoTestClient {
    public static void main(String[] args) {
        CalcDemo calcDemo = new CalcDemo();

        int para1 = 10;
        int para2 = 0;
        Method[] method = calcDemo.getClass().getMethods();
        AtomicInteger bugCount = new AtomicInteger();
        String filepath = "BugReport_" + (DateUtil.format(new Date(), "yyyy_MM_dd_HH_mm_ss")) + ".txt";
        for (int i = 0; i < method.length; i++) {
            if (method[i].isAnnotationPresent(CustomTest.class)) {
                try {
                    method[i].invoke(calcDemo, para1, para2);
                } catch (Exception e) {
                    bugCount.incrementAndGet();
                    log.info("异常名称:{}, 异常原因:{}", e.getCause().getClass().getSimpleName(), e.getCause().getMessage());

                    FileUtil.writeString("异常方法: " + method[i].getName() + "\n", filepath, "UTF-8");
                    FileUtil.appendString("异常名称: " + e.getCause().getClass().getSimpleName() + "\n", filepath, "UTF-8");
                    FileUtil.appendString("异常原因: " + e.getCause().getMessage() + "\n", filepath, "UTF-8");


                } finally {
                    FileUtil.appendString("异常数量:" + bugCount.get() + "\n", filepath, "UTF-8");
                }
            }
        }

    }
}
